package com.fengye.model;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Netty异步调用返回结果包装类
 *
 * @Author fengye
 * @Date 2023/2/14 20:03
 **/
public class RemoteResponseWrapper {

    /** 存储返回结果的阻塞队列, 正常来说队列里只有一个结果 */
    private BlockingQueue<RemoteResponse> responseQueue = new ArrayBlockingQueue<RemoteResponse>(1);

    /** 返回结果时间 */
    private long responseTime;

    /**
     * 计算该结果是否已经过期
     * @return
     */
    public boolean isExpire() {
        RemoteResponse response = responseQueue.peek();
        if(response == null){
            return false;
        }

        long timeout = response.getInvokeTimeout();
        if((System.currentTimeMillis() - responseTime) > timeout) {
            return true;
        }
        return false;
    }

    public static RemoteResponseWrapper of() {
        return new RemoteResponseWrapper();
    }

    public BlockingQueue<RemoteResponse> getResponseQueue() {
        return responseQueue;
    }

    public void setResponseQueue(BlockingQueue<RemoteResponse> responseQueue) {
        this.responseQueue = responseQueue;
    }

    public long getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(long responseTime) {
        this.responseTime = responseTime;
    }
}
